/*
 * 文件名：AdminPayBillServiceImpl.java	 
 * 时     间：下午12:20:21
 * 作     者：lyf       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cz073586.tools.date.DateFormatString;
import com.cz073586.tools.date.DateTools;
import com.cz073586.tools.util.Tools;
import com.local.common.GateWayConstans;
import com.local.common.MessageContains;
import com.local.dao.mapers.AdminPayBillMapper;
import com.local.dao.mapers.CarParkInMapper;
import com.local.dao.mapers.LocalCarInRepertoryMapper;
import com.local.dao.mapers.LocalChargeBytimeMapper;
import com.local.dao.mapers.LocalEquipmentMapper;
import com.local.dao.mapers.LocalScreenMapper;
import com.local.dao.mapers.LocalStationMapper;
import com.local.entity.ex.AdminPayBillEx;
import com.local.entity.vo.CarInManualOpenVo;
import com.local.entity.vo.CarParkInOverVo;
import com.local.entity.vo.ParkwellPayBillVo;
import com.local.manager.cfgManager.ConfigManager;
import com.local.model.AdminCarInBill;
import com.local.model.AdminPayBill;
import com.local.model.AdminPayBillExample;
import com.local.model.CarParkIn;
import com.local.model.CarParkInExample;
import com.local.model.Crossing;
import com.local.model.LocalCarInRepertory;
import com.local.model.LocalChargeBytime;
import com.local.model.LocalChargeBytimeCopy;
import com.local.model.LocalChargeBytimeExample;
import com.local.model.LocalChargeBytimeType;
import com.local.model.LocalEquipment;
import com.local.model.LocalStation;
import com.local.model.Screen;
import com.local.page.AdminPayBillPage;
import com.local.service.face.AdminCarInBillService;
import com.local.service.face.AdminPayBillService;
import com.local.service.face.CarInAndOutService;
import com.local.service.face.CrossingService;
import com.local.service.face.LocalChargeByTimeCopyService;
import com.local.service.face.LocalChargeByTimeService;
import com.local.service.face.LogService;
import com.local.service.face.ParkwellPayBillService;
import com.qianhai.common.dao.face.BaseMapper;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.service.impl.BaseServiceImpl;
import com.qianhai.common.utils.BeanUtil;
import com.qianhai.common.utils.ShiroUtil;
import com.qianhai.common.utils.UUIDGenerateUtil;
import com.tt.system.dao.mapers.UserMapper;
import com.tt.system.model.User;

import common.cz0735.coustomer.mapers.CoustomerInfoMapper;
import common.cz0735.coustomer.model.CoustomerInfo;
import common.cz0735.coustomer.model.CoustomerInfoExample;

/**
 * 类描述:这里是类描述
 * 
 * @ClassName: AdminPayBillServiceImpl
 * @author Administrator
 * @date 2015年9月26日 下午12:20:21
 */
@Service
public class AdminPayBillServiceImpl extends
		BaseServiceImpl<AdminPayBillExample, AdminPayBill, String> implements
		AdminPayBillService
{
	@Autowired
	private AdminPayBillMapper adminPayMappeer;
	
	@Autowired
	private CoustomerInfoMapper coustomerInfoMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private LocalStationMapper localStationMapper;
	
	@Autowired
	private LocalScreenMapper localScreenMapper;
	
	@Autowired
	private CrossingService crossingServer;
	
	@Autowired
	private CarParkInMapper carinMapper;
	
	@Autowired
	private AdminCarInBillService adminCarInBillService;
	
	@Autowired
	private LocalChargeByTimeCopyService localChargeByTimeCopyService;
	
	@Autowired
	private CarInAndOutService carInAndOutService;
	
	@Autowired
	private ParkwellPayBillService payBilllService;
	
	@Override
	public Integer addAdminPayBill(AdminPayBill adminPayBill)
			throws BaseException
	{
		try
		{
			if (Tools.isEmpty(adminPayBill.getLonginUserUuid()))
			{
				throw new BaseException("50000-00001-00003");
			}
			
			if (Tools.isEmpty(adminPayBill.getStationUuid()))
			{
				throw new BaseException("50000-00001-00005");
			}
			
			// 获取设备编号
			if (Tools.isEmpty(adminPayBill.getCrossingId()))
			{
				throw new BaseException("50000-00001-00035");
			}
			
			Crossing crssing = crossingServer.getCrossinByid(adminPayBill
					.getCrossingId());
			List<LocalEquipment> localequimentList = crossingServer
					.queryEquimentByCrossionId(adminPayBill.getCrossingId());
			adminPayBill.setUuid(UUIDGenerateUtil.generate());
			if (!localequimentList.isEmpty())
			{
				adminPayBill.setExchangeNo(localequimentList.get(0)
						.getExchangeNo());
			}
			adminPayBill.setPayTime(new Date());
			adminPayBill.setAttr1(Tools.isEmpty(crssing.getStatus()) ? null
					: crssing.getStatus().toString());
			adminPayMappeer.insertSelective(adminPayBill);
			
			// 开闸
			crossingServer.openGATE(adminPayBill.getCrossingId(),
					adminPayBill.getStationUuid());
			// PushAdminRunble push = new PushAdminRunble(adminPayBill,
			// list.get(0).getExchangeNo(), list.get(0).getCameraNo());
			// ThreadPoolUtil.instance().execute(push);
			
		} catch (Throwable t)
		{
			throw new BaseException(t.getMessage(), t);
		}
		return null;
	}
	
	/**
	 * 分页查询强制开闸信息 方法描述：
	 * 
	 * @Title: queryAdminPayBillByPage
	 * @param @param page
	 * @param @return
	 * @return List<AdminPayBill>
	 * @user Administrator 2015年10月2日
	 * @throws
	 */
	@Override
	public List<AdminPayBill> queryAdminPayBillByPage(
			AdminPayBillPage<AdminPayBill> page)
	{
		
		try
		{
			Integer recordCount = adminPayMappeer.countAllByPage(page);
			page.setTotalRecords(recordCount);
			List<AdminPayBill> list = adminPayMappeer.queryByPage(page);
			
			// 转换岗亭id为岗亭名
			List<AdminPayBill> listEx = new ArrayList<AdminPayBill>();
			
			for (AdminPayBill paybill : list)
			{
				AdminPayBillEx ex = new AdminPayBillEx();
				BeanUtil.copyProperties(ex, paybill);
				// 岗亭对象
				LocalStation station = localStationMapper
						.selectByPrimaryKey(paybill.getStationUuid());
				if (!Tools.isEmpty(station))
				{
					ex.setStateName(station.getStationName());
				}
				
				// 登录用户
				//
				// CoustomerInfo coustomerInfo = coustomerInfoMapper
				// .selectByPrimaryKey(paybill.getLonginUserUuid());
				
				CoustomerInfoExample custEx = new CoustomerInfoExample();
				custEx.or()
						.andLoginUserUuidEqualTo(paybill.getLonginUserUuid());
				List<CoustomerInfo> custList = coustomerInfoMapper
						.selectByCondition(custEx);
				
				if (!Tools.isEmpty(custList) && !custList.isEmpty())
				{
					ex.setLoginUserName(custList.get(0).getName());
				} else
				{
					User user = userMapper.selectByPrimaryKey(paybill
							.getLonginUserUuid());
					ex.setLoginUserName(Tools.isEmpty(user) ? "admin" : user
							.getLoginName());
				}
				
				listEx.add(ex);
			}
			
			page.setData(listEx);
			return listEx;
		} catch (Exception e)
		{
			throw new BaseException("", e);
		}
	}
	
	/**
	 * 根据登录用户查询开闸的记录数 方法描述：
	 * 
	 * @Title: queryAdminPayBillCountByLoginUserId
	 * @param @param loginId
	 * @param @return
	 * @return Integer
	 * @user Administrator 2015年10月4日
	 * @throws
	 */
	public Integer queryAdminPayBillCountByLoginUserId(AdminPayBill adminPayBill)
	{
		
		return adminPayMappeer.querycountAdminPayBill(adminPayBill);
	}
	
	@Override
	public BaseMapper<AdminPayBillExample, AdminPayBill, String> getMapper()
	{
		
		return adminPayMappeer;
	}
	
	@Override
	public Integer carInManualOpen(CarInManualOpenVo vo,
			AdminCarInBill adminCarInBill) throws BaseException
	{
		try
		{
			
			CarParkIn carInInfo = carinMapper.selectByPrimaryKey(vo
					.getCarinUUid());
			
			if (!Tools.isEmpty(carInInfo))
			{
				Screen screen = localScreenMapper
						.queryScreenByCamelDeviceId(carInInfo.getEquipment());
				vo.setScreen(screen);
				//设置手动开闸标记
				carInInfo.setOpenMode(GateWayConstans.MANUAL);
				
				vo.setCarInInfo(carInInfo);
				carInInfo.setStatus(1);
				carInInfo.setCarType(!Tools.isEmpty(vo.getCarType()) ? vo
						.getCarType() : null);
				carInInfo.setAttr1(!Tools.isEmpty(vo.getPayType()) ? vo
						.getPayType() : null);
				carInInfo.setOpenTime(DateTools.dateToString(new Date(),
						DateFormatString.yyyy_MM_dd_HH_mm_ss_H));
				carInInfo.setCarNo(vo.getCarNo());
				
				LocalChargeBytimeType ltc = carInAndOutService
						.makeCarNoType(carInInfo);
				carInInfo.setBrand(!Tools.isEmpty(ltc)
						&& !Tools.isEmpty(ltc.getType()) ? ltc.getType()
						: "102");
				
				carinMapper.updateByPrimaryKeySelective(carInInfo);
				
				CarParkInOverVo carInVo = new CarParkInOverVo();
				carInInfo.setType(vo.getCarType());
				carInVo.setCarparkIn(carInInfo);
				carInAndOutService.inserCarInRepertory(carInVo);
				
				List<Crossing> listCrs = crossingServer
						.queryCrossingByEquimentNo(carInInfo.getEquipment());
				String crsoId = listCrs.isEmpty() ? null : listCrs.get(0)
						.getUuid();
				
				adminCarInBill.setUuid(UUIDGenerateUtil.generate());
				adminCarInBill.setCarNo(carInInfo.getCarNo());
				adminCarInBill.setInTime(carInInfo.getCreateTime());
				adminCarInBill.setCrossingId(crsoId);
				adminCarInBill.setExchangeNo(carInInfo.getExchangeUuid());
				adminCarInBill.setLonginUserUuid(ShiroUtil.getCurrentUser()
						.getUuid());
				
				adminCarInBillService.addAdminCarInBill(adminCarInBill);
				
				// TODO liy 删除多余进场记录
				if (carInInfo.getStatus() == 1
						&& !Tools.isEmpty(carInInfo.getUuid())
						&& !carInInfo.getCarNo().equalsIgnoreCase("无效车牌"))
				{
					CarParkInExample inEx = new CarParkInExample();
					inEx.or().andUuidNotEqualTo(carInInfo.getUuid())
							.andCarNoEqualTo(carInInfo.getCarNo());
					carinMapper.deleteByCondition(inEx);
				}
				
				// 删除滚动计费表信息,如果已经开闸则删除非当天滚动计费信息
				if (!carInInfo.getCarNo().equals("无效车牌"))
				{
					ParkwellPayBillVo billVo = new ParkwellPayBillVo();
					billVo.setCarNo(carInVo.getCarparkIn().getCarNo());
					payBilllService.delRepaetBillNotInDay(billVo);
				}
				
				// 添加车辆计费副本
				// 计费
				List<LocalChargeBytimeCopy> lis = localChargeByTimeCopyService
						.addRuleCopy(carInInfo);
				vo.setListCopy(lis);
			}
			/*
			 * if (!Tools.isEmpty(carInRepertory)) {
			 * carInRepertory.setCarNo(vo.getCarNo()); if
			 * (!carInRepertory.getCarTypeUuid().equals(vo.getCarType())) {
			 * carInRepertory.setCarTypeUuid(vo.getCarType());
			 * if(Tools.isEmpty(vo.getCarType())){
			 * carInRepertory.setCarTypeUuid(ConfigManager.instance()
			 * .getDefaultCarType()); } LocalChargeBytimeExample ex = new
			 * LocalChargeBytimeExample();
			 * ex.or().andCarTypeEqualTo(carInRepertory.getCarTypeUuid());
			 * List<LocalChargeBytime> list = localChargeBytimeMapper
			 * .selectByCondition(ex); String chargeUuids = ""; for
			 * (LocalChargeBytime localChargeBytime : list) { chargeUuids +=
			 * localChargeBytime.getChargeBytimeUuid() + ","; }
			 * carInRepertory.setChargeUuid(Tools .isEmpty(chargeUuids) ? null :
			 * chargeUuids .substring(0, chargeUuids.length() - 1)); }
			 * localCarInRepertoryMapper
			 * .updateByPrimaryKeySelective(carInRepertory); }
			 */
			
		} catch (Throwable t)
		{
			throw new BaseException("手动开闸失败", t);
		}
		return null;
	}
	
}
